import { useEditorStore } from '@/store/editor';

const getExtent = (str: string) => {
  let extent = [];
  if (str.includes('EPSG')) {
    extent = str.replace('[EPSG:4326]', '').trim().split(',');
  } else {
    extent = str.trim().split(',');
  }
  extent = extent.map((v: string) => Number(v)).sort((a: number, b: number) => a - b);
  const Xmin = extent[2],
    Xmax = extent[3],
    Ymin = extent[0],
    Ymax = extent[1];

  return [Xmin, Ymin, Xmax, Ymax];
};

export default (layer: any) => {
  let data = {};
  // let configJson = JSON.parse(stageStore?.sceneDetail?.configJson);
  let configJson = JSON.parse(layer?.data?.tifConfig || '{}');
  if (configJson.Method === 'XyzCut') {
    const extent = configJson.Extent ? getExtent(configJson.Extent) : null;
    data = {
      enable: true,
      source: 'fileserver',
      maximumLevel: 18,
      minimumLevel: 1,
      brightness: 1,
      saturation: 1,
      contrast: 1,
      extent: extent,
      flyTo: true,
    };
  } else {
    const extent = configJson.Extent ? getExtent(configJson.Extent) : null;
    const arr = layer?.dataSourceUrl.split('?');
    const url = arr[0] || [],
      param: any = {};
    if (arr[1] && arr[1].split('&').length > 0) {
      arr[1].split('&').forEach((ele: string) => {
        param[ele.split('=')[0]] = ele.split('=')[1];
      });
    }
    data = {
      enable: true,
      source: 'fileserver',
      dataSourceUrl: `/${url}?`,
      layers: param?.layers || '',
      format: param?.format || 'image/png',
      srs: param?.srs || 'EPSG:4326',
      serviceVer: param?.serviceVer || '1.1.0',
      maximumLevel: 18,
      minimumLevel: 1,
      brightness: 1,
      saturation: 1,
      contrast: 1,
      extent,
    };
  }

  return data;
};
